#include "rcc.h"
#include "py32f0xx_hal.h"

// 系统时钟配置函数：24MHz HSI
void SystemClock_Config(void)
{
    // 振荡器配置
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI; // 选择振荡器HSE,HSI,LSI
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;                                                                     // 开启HSI
    RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;                                                                     // HSI 1分频
    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_24MHz;                                            // 配置HSI时钟
    RCC_OscInitStruct.HSEState = RCC_HSE_OFF;                                                                    // 关闭HSE
    /*RCC_OscInitStruct.HSEFreq = RCC_HSE_16_32MHz;*/
    RCC_OscInitStruct.LSIState = RCC_LSI_OFF; /* 关闭LSI */
    HAL_RCC_OscConfig(&RCC_OscInitStruct);

    // 时钟源配置
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1; // 选择配置时钟 HCLK,SYSCLK,PCLK1
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;                                         // 选择HSI作为系统时钟
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;                                             // AHB时钟 1分频
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;                                              // APB时钟 1分频
    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
}
